import { useTable, LIST_TYPE } from '@crud/index';
import { formatParamsSelectVal } from '@/utils';
import { getList } from '@/api/crm-content/article';
import dayjs from 'dayjs';

// 公告类型
export const articleTypeOptions: any = [
  { label: '全部', value: -1 },
  { label: '行业资讯', value: 1 },
  { label: '产品新知', value: 2 }
];

// 发送对象
export const statusOptions: any = [
  { label: '全部', value: -1 },
  { label: '待发布', value: 0 },
  { label: '已发布', value: 1 },
  { label: '定时发布', value: 2 }
];
// // 类型
// export const typeMap: any = toolOptions2Map(typeOptions);

// // 发送对象
// export const sendObjectMap: any = toolOptions2Map(sendObjectOptions);
export const statusStyleMap: Record<string, any> = {
  待发布: '#909399',
  已发布: '#67C23A',
  定时发布: '#E6A23C'
};
// 表格
export const useTableList = (options: any = {}) => {
  const columns = [
    { label: '封面', prop: 'cover', width: 200, render: 'cover', fixed: 'left' },
    { label: '标题', prop: 'title', minWidth: 230, render: 'title' },
    { label: '作者', prop: 'author', minWidth: 160 },
    { label: '类型', prop: 'article_type', minWidth: 120 },
    { label: '阅读量', prop: 'reading', minWidth: 120 },
    { label: '状态', prop: 'status', minWidth: 120, render: 'status' },
    { label: '发布时间', prop: 'publish_time', width: 177, type: 'dateTime' },
    { label: '发布人', prop: 'user_name', minWidth: 120 },
    { label: '操作', align: 'center', width: 200, render: 'actions', fixed: 'right' }
  ];

  return useTable({
    type: LIST_TYPE.CONTENT,
    params: {
      article_type: -1,
      status: -1,
      title: undefined,
      _timeArr: [],
      author: undefined,
      user_name: undefined
    },
    columns,
    getParams: ({ _timeArr = [], ...params }) => {
      // 处理时间参数
      if (_timeArr?.length) {
        params.begin_time = dayjs(_timeArr[0]).startOf('day').format('X');
        params.end_time = dayjs(_timeArr[1]).endOf('day').format('X');
      }
      return {
        ...params,
        ...formatParamsSelectVal(
          [
            ['status', -1],
            ['article_type', -1]
          ],
          params
        )
      };
    },
    getList: ({ params, data }: any) => getList(params, data)
  });
};
